package com.igeek.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.car.entity.Reschedules;
import com.igeek.car.mapper.ReschedulesMapper;
import com.igeek.car.service.ReschedulesService;
import com.igeek.car.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 改签记录表 服务实现类
 */
@Service
public class ReschedulesServiceImpl extends ServiceImpl<ReschedulesMapper, Reschedules> implements ReschedulesService {

    @Autowired
    private ReschedulesMapper reschedulesMapper;

    // 分页条件查询改签记录
    @Transactional(readOnly = true)
    @Override
    public Page<Reschedules> findPage(PageQueryVO vo) {
        // 1.封装分页数据
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        Page<Reschedules> page = Page.of(currentPage, pageSize);

        // 2.封装查询条件
        String queryString = vo.getQueryString();
        LambdaQueryWrapper<Reschedules> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasLength(queryString), Reschedules::getOrderID, queryString)
                .or()
                .like(StringUtils.hasLength(queryString), Reschedules::getOldSchedule, queryString);

        // 3.执行分页条件查询
        return this.baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    // 删除改签记录
    @Transactional
    @Override
    public boolean del(Integer id) {
        // 1.检查改签记录是否存在
        if(ObjectUtils.isEmpty(id) || this.baseMapper.selectById(id) == null) {
            throw new RuntimeException("改签记录不存在！");
        }

        // 2.执行删除操作
        int result = this.baseMapper.deleteById(id);
        return result > 0;
    }
}